package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfx.entity.*;
import com.wfx.mapper.WxbGoodsSkuMapper;
import com.wfx.mapper.WxbOrderItemsMapper;
import com.wfx.mapper.WxbOrderLogMapper;
import com.wfx.mapper.WxbOrderMapper;
import com.wfx.service.IWxbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-23
 */
@Service
@Transactional//开启事务
public class WxbOrderServiceImpl extends ServiceImpl<WxbOrderMapper, WxbOrder> implements IWxbOrderService {


    @Autowired
    private WxbGoodsSkuMapper wxbGoodsSkuMapper;

    @Autowired
    private WxbOrderMapper wxbOrderMapper;


    @Autowired
    private WxbOrderItemsMapper wxbOrderItemsMapper;

    @Autowired
    private WxbOrderLogMapper wxbOrderLogMapper;

    @Override
    public List<Map> statOrdercuntPerDay() {
        return wxbOrderMapper.statOrdercuntPerDay();
    }

    @Override
    public List<Map> statOrderCuntPerMoth() {
        return wxbOrderMapper.statOderCuntPerMoth();
    }


    @Override
    public List<Map> statOderCuntPerMoney() {
        return wxbOrderMapper.statOderCuntPerMoney();
    }

    @Override
    public PageResult<WxbOrder> search(PageDTO<WxbOrder> pageDTO) {
        if (pageDTO == null) {
            return new PageResult<WxbOrder>(false, "查询失败", null, null, null);
        }
        IPage<WxbOrder> iPage = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        WxbOrder data = pageDTO.getData();
        if (data == null) {
            data = new WxbOrder();
        }
        IPage<WxbOrder> pageInfo = wxbOrderMapper.findOrder(iPage, data);
        PageResult pageResult = new PageResult<>();
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;
    }


    //添加订单
    @Override
    public Result saveOrder(OrderDto orderDto) {
        if (orderDto == null) {
            return new Result(false, "参数不合法");
        }

        //1:判断库存够不够
        WxbGoodsSku wxbGoodsSku = wxbGoodsSkuMapper.selectById(orderDto.getSkuId());
        if (wxbGoodsSku == null) {
            return new Result(false, "没有该商品");
        }

        if (orderDto.getBuyNum() > wxbGoodsSku.getNum()) {
            return new Result(false, "库存不足");
        }


        //2.插入订单主表信息
        WxbOrder order = new WxbOrder();
        String orderId = genOrder();
        order.setOrderId(orderId);
        order.setBuyNum(orderDto.getBuyNum());
        order.setGoodId(wxbGoodsSku.getGoodsId() + "");  //spuId
        order.setOrderTime(new Date());
        order.setState(0);//状态 0：未支付 1：已支付 2：已关闭
        order.setUpdateTime(new Date());

        //收件人信息
        WxbAddress address = orderDto.getAddress();
        order.setBuyerName(address.getLocName());
        order.setBuyerPhone(address.getLocTel());
        int insert = wxbOrderMapper.insert(order);


        //3.保存订单明细表
        WxbOrderItems items = new WxbOrderItems();
        items.setBuyNum(orderDto.getBuyNum());
        items.setCreateTime(new Date());
        items.setGoodsId(wxbGoodsSku.getGoodsId() + "");
        items.setOrderId(orderId); //订单id
        items.setSkuId(wxbGoodsSku.getId() + "");
        items.setSkuPrice(wxbGoodsSku.getPrice() + "");
        items.setUpdateTime(new Date());
        int insert1 = wxbOrderItemsMapper.insert(items);


        //插入订单日志
        WxbOrderLog log = new WxbOrderLog();
        log.setCreatetime(new Date());
        Double fee = Double.parseDouble(items.getSkuPrice()) * 100;
        log.setFee(fee.intValue() * items.getBuyNum());
        log.setOrderState(0);
        log.setUpdatetime(new Date());
        log.setOrderId(orderId);
        wxbOrderLogMapper.insert(log);


        //减库存
        wxbGoodsSku.setNum(wxbGoodsSku.getNum() - orderDto.getBuyNum());
        wxbGoodsSkuMapper.updateById(wxbGoodsSku);


        return new Result(true, "下单成功", orderId);
    }


    private static String genOrder() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String orderId = format.format(new Date());

        return orderId;
    }


}
